﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace WorkFlow.Engine.NSParser
{
    public class WfParameterAnalysis
    {
        private readonly string[] _keyword =
            {
                "PUBLIC", "PRIVATE", "CONST", "IF", "ELSE", "CASE", "SWITCH", "BREAK",
                "CONTINUE", " DO"
            }; //关键字

        public List<string> GetParams(string express) //文件读取
        {
            var Params = new List<string>();
            Char[] ch = express.ToCharArray();

            string tempstr = null; //重组单词

            string num = null; //重组数字

            for (int i = 0; i < ch.Length - 1; i++)
            {
                if (ch[i] != ' ')
                {
                    if ((ch[i] > 'A' && ch[i] < 'z') || (ch[i] <= '9' && ch[i] >= '0' && tempstr != null) ||
                        (ch[i] == '_'))
                    {
                        if (ch[i] == 'e' && num != null)
                        {
                            num += "e";
                            continue;
                        }

                        tempstr += ch[i].ToString(CultureInfo.InvariantCulture);
                        if (ch[i + 1] > 'z' || (ch[i + 1] < 'A' && ch[i + 1] > '9') || (ch[i + 1] < '0'))
                        {
                            bool tempbool = false;

                            for (int j = 0; j < 10; j++)
                            {
                                if (tempstr.ToUpper().Equals(_keyword[j]))
                                    tempbool = true;
                            }

                            if (!tempbool)
                            {
                                if (!Params.Contains(tempstr)) Params.Add(tempstr);
                            }
                            tempstr = null;
                        }
                    } //关键字判定

                    else if (ch[i] >= '0' && ch[i] <= '9')
                    {
                        num += ch[i].ToString(CultureInfo.InvariantCulture);
                        if (ch[i + 1] == '.')
                        {
                            num += ".";
                            i++;
                        }
                    }
                }
            }
            return Params;
        }
    }
}